﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WorkStation.Frameworks.Core;
using WorkStation.Frameworks.Core.Extensions;

using WorkStation.Frameworks.EasyBus.MessageQueue;
using WorkStation.Frameworks.EasyBus.DbModel;
using WorkStation.Frameworks.EasyBus.Extensions;
using WorkStation.Frameworks.EasyBus.Pipeline;
using WorkStation.Frameworks.EasyBus.Pipeline.Extensions;
using WorkStation.Frameworks.EasyBus.Pipeline.Handles;

namespace WorkStation.Frameworks.EasyBus.Handles
{
    public class DefaultPublishHandle : Handle
    {
        public IEndpoint TargetEndpoint { get; set; }
        public string SubscribeBusName { get; set; }
        public string SubscribeServerNode { get; set; }
        public string SubscribeName { get; set; }

        public IChannel Channel { get; set; }

        protected override HandleResult InnerExecute(ActionContext context)
        {
            PublishHandleResult res = new PublishHandleResult(context.Input, this);

            Message msg = context.Input as Message;
            
            try
            {
                if (this.Channel == null)
                {
                    this.Channel = this.TargetEndpoint.GetChannel();
                }

                this.Channel.WriteMsg(msg);

                res.PublishResult.Add(new MessagePublishLog() 
                {
                    MessageId = msg.MessageId,
                    IsSuccess = true,
                    SubscribeBusNodeName = this.SubscribeServerNode,
                    SubscribeBusName = this.SubscribeBusName,
                    SubscribeName = this.SubscribeName,
                    Mq = this.TargetEndpoint.ToEndpointString(),
                    PublishTime = DateTime.Now
                });
            }
            catch (Exception ce) 
            {
                res.PublishResult.Add(new MessagePublishLog() 
                {
                    MessageId = msg.MessageId,
                    IsSuccess = false,
                    SubscribeBusNodeName = this.SubscribeServerNode,
                    SubscribeBusName = this.SubscribeBusName,
                    SubscribeName = this.SubscribeName,
                    Error = ce.ToString(),
                    Mq = this.TargetEndpoint.ToEndpointString(),
                    PublishTime = DateTime.Now
                });
            }
            
            return res;
        }

        public static string GetHandleName(SubscribeInfo info) 
        {
            return "BusName:{0}^NodeName:{1}^SubscribeName:{2}".Fmt(info.BusName, info.NodeName, info.Name);
        }
    }

    public class PublishHandleResult : HandleResult 
    {
        public List<MessagePublishLog> PublishResult { get; set; }

        public PublishHandleResult(object input, IHandle handle) : base(input, handle)
        {
            this.PublishResult = new List<MessagePublishLog>();
        }
    }
}
